<template>
  <div class="app-container" style="width:100%;height:100%;">
    <div class="block" style="width:100%;height:100%;">
      <el-tree
        id="el-tree"
        ref="tree"
        class="filter-tree"
        :data="data2"
        icon-class="none"
        node-key="id"
        :highlight-current="true"
        :props="defaultProps"
        default-expand-all
        :expand-on-click-node="false"
        show-checkbox
      />
    </div>
    <span slot="footer" class="dialog-footer">
      <div style="text-align:center;margin-top:20px;">
        <el-button type="primary" @click="onSave">确定</el-button>
        <el-button type="danger" class="quxiao_btn" @click="closePage()">取消</el-button>
      </div>
    </span>
  </div>
</template>

<script>

import { getTreeList } from '@/api/usersManualCatalog'
import { downloadWord } from '@/api/usersManualFile'

export default {
  name: 'Zzjg',
  props: { // 第二种方式
    xiangmu: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      data: [],
      data2: [],
      ids: '',
      defaultProps: {
        children: 'children',
        label: 'name'
      }
    }
  },
  created() {
    this.getTreeList()
  },
  methods: {
    getTreeList() { // 加载列表
      getTreeList({ xiangmuId: this.xiangmu }).then(response => {
        this.data = response.data

        this.data2 = this.buildTree2(this.data)
      }).catch(response => {
      })
    },
    buildTree2(data) {
      var rdata = []
      for (let i = 0; i < data.length; i++) {
        var e1 = data[i]
        if (e1.parentId === '-1') {
          rdata.push(e1)
        }
        for (let j = 0; j < data.length; j++) {
          var e2 = data[j]
          if (e1.parentId === e2.id) {
            if (!e2.children) {
              e2.children = []
            }
            e2.children.push(e1)
          }
        }
      }
      return rdata
    },
    onSave() {
      var ids = ''
      var flag = ''
      var data = this.$refs.tree.getCheckedNodes()
      if (data.length !== 0) {
        for (var i = 0; i < data.length; i++) {
          if (data[i].flag === 'WD') {
            ids = ids + data[i].id + ','
          }
        }
      } else {
        flag = 'XM'
        ids = 'null'
      }
      const loading = this.$loading({
        lock: true,
        text: 'Loading',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      })

      downloadWord({ ids: ids, flag: flag }).then(response => {
        window.open(response.data, '_blank')

        loading.close()
      }).catch(response => {
        loading.close()
      })
    },
    closePage() {
      this.$emit('update:visible', false)
    }
  }
}
</script>
